package com.dayu.rabbitmq.confirm;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.dayu.rabbitmq.util.MQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author dayu
 * @date 2019/02/20
 * @Describe 单条信息confirm牛
 */
public class ConfirmSend {
    private static String QUEUE_NAME = "test_confirm";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = MQUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 注意：同一个队列只支持一个模式,如果同一个队列已经选择了事务模式就不能选择confirm模式
        channel.confirmSelect();
        String msg = "hello confirm msg";
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());

        if(!channel.waitForConfirms()) {
            System.out.println("msg send failed");
        }else {
            System.out.println("msg send success");            
        }
        channel.close();
        connection.close();

    }
}
